package com.linkage.seapeak.net;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.seapeak.pojo.CrediterUser;
import com.linkage.seapeak.pojo.VoteGroup;
import com.linkage.seapeak.pojo.VoteUserListBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import retrofit2.http.Field;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 表决用的接口请求
 */
public class VoteNetRequest {

    public static void getVoteGroup(String userToken, String voteId, String status,Boolean defaultAgreed, final ResultCallBack<List<VoteGroup>> callBack) {
        Network.getAPIService()
                .getVoteGroup(userToken, voteId, status, defaultAgreed,1, 100)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");

                            List<VoteGroup> voteGroups = new ArrayList<>();
                            for (int i = 0;i < groupArray.size();i++) {
                                VoteGroup group = new Gson().fromJson(groupArray.get(i).toString(), VoteGroup.class);
                                voteGroups.add(group);
                            }

                            if (callBack != null)
                                callBack.onSuccess(voteGroups);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    /*public static void getVoteMember(String userToken, String voteId, String projectId, String groupId, String status, final ResultCallBack<List<CrediterUser>> callBack) {
        Network.getAPIService()
                .getVoteMember(userToken, voteId, projectId, groupId, status, "1", "200")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");

                            List<CrediterUser> crediterUsers = new ArrayList<>();
                            for (int i = 0;i < groupArray.size();i++) {
                                CrediterUser user = new Gson().fromJson(groupArray.get(i).toString(), CrediterUser.class);
                                crediterUsers.add(user);
                            }

                            if (callBack != null)
                                callBack.onSuccess(crediterUsers);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }*/

    public static void getEditGroupList(String userToken, String voteId, final ResultCallBack<List<VoteGroup>> callBack) {
        Network.getAPIService()
                .getEditGroupList(userToken, voteId, "1", "100")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");

                            List<VoteGroup> voteGroups = new ArrayList<>();
                            for (int i = 0;i < groupArray.size();i++) {
                                VoteGroup group = new Gson().fromJson(groupArray.get(i).toString(), VoteGroup.class);
                                voteGroups.add(group);
                            }

                            if (callBack != null)
                                callBack.onSuccess(voteGroups);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    //    如果为首次进入表决人员列表页面需填入此参数用于区分分组数据和非分组数据，后续在列表页面加载更多人员将该参数置空。即该参数为进入页面初始化参数
    public static void showVoteMemberTable(String userToken, String voteId, String groupId,
                                               String projectId, String voteRule, final ResultCallBack<List<CrediterUser>> callBack) {
        Network.getAPIService()
                .getShowVoteMember(userToken, projectId,voteId, groupId,  voteRule,"1", "500" )
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data").getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");

                            List<CrediterUser> crediterUsers = new ArrayList<>();
                            if (groupArray != null) {
                                for (int i = 0;i < groupArray.size();i++) {
                                    CrediterUser user = new Gson().fromJson(groupArray.get(i).toString(), CrediterUser.class);
                                    crediterUsers.add(user);
                                }
                            }

                            if (callBack != null)
                                callBack.onSuccess(crediterUsers);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    //    如果为首次进入表决人员列表页面需填入此参数用于区分分组数据和非分组数据，后续在列表页面加载更多人员将该参数置空。即该参数为进入页面初始化参数
    public static void showVoteGroupMemberTable(String userToken, String voteId,
                                                    String projectId, String voteRule, final ResultCallBack<List<VoteGroup>> callBack) {
        Network.getAPIService()
                .getShowVoteMember(userToken, projectId,voteId, "",   voteRule,"1", "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonElement element = json.get("data");
                            JsonArray groupArray = null;
                            if (element != null && !element.isJsonNull())
                                groupArray = element.getAsJsonObject().get("data").getAsJsonArray();

                            List<VoteGroup> voteGroups = new ArrayList<>();
                            if (groupArray != null) {
                                voteGroups = new ArrayList<>();
                                for (int i = 0;i < groupArray.size();i++) {
                                    VoteGroup group = new Gson().fromJson(groupArray.get(i).toString(), VoteGroup.class);
//                                    group.setMemberCount(10);
                                    voteGroups.add(group);
                                }
                            }
                            try {
                                int totalCount = json.getAsJsonObject("data").get("memberCount").getAsInt();
//                                bean.totalCount = totalCount;
                            }catch (Exception ex)
                            {
//                                bean.totalCount = 0;
                                ex.printStackTrace();
                            }

                            if (callBack != null)
                                callBack.onSuccess(voteGroups);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }


//    public static void addVoteMember(String userToken, String voteId, String projectId, String groupId, List<CrediterUser> userList, final ResultCallBack<String> callBack) {
//        JsonArray array = new JsonArray();
//        for (CrediterUser user : userList) {
//            JsonObject item = new JsonObject();
//            item.addProperty("creditorId", user.getCreditorid());
//            item.addProperty("confirmedMoney", user.getConfirmedMoney());
//            item.addProperty("voteMoney", user.getVoteMoney());
//            array.add(item);
//        }
//        Network.getAPIService()
//                .addVoteMember(userToken, voteId, projectId, groupId, array.toString())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseSubscriber<JsonObject>() {
//                    @Override
//                    public void onNext(JsonObject json) {
//                        int code = json.get("code").getAsInt();
//                        if (code != 0) {
//                            if (callBack != null)
//                                callBack.onFailed(json.get("msg").getAsString());
//                        } else {
//                            if (callBack != null)
//                                callBack.onSuccess("请求成功");
//                        }
//                    }
//
//                    @Override
//                    public void onCompleted() {
//                        super.onCompleted();
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        super.onError(e);
//                        if (callBack != null)
//                            callBack.onFailed("请求失败");
//                    }
//                });
//    }

    public static void deleteVoteMember(String userToken, String voteId, String tmpId, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .deleteVoteMember(userToken, voteId, tmpId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void clearVoteMember(String userToken, String voteId, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .clearVoteMember(userToken, voteId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void addVoteGroup(String userToken, String voteId, String name, boolean ordinary,
                                    boolean guarantee, boolean other, final ResultCallBack<VoteGroup> callBack) {
        Network.getAPIService()
                .addVoteGroup(userToken, voteId, name, ordinary, guarantee, other)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            VoteGroup tempGroup = new Gson().fromJson(json.get("data").toString(), VoteGroup.class);
                            if (callBack != null)
                                callBack.onSuccess(tempGroup);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void editVoteGroup(String userToken, String voteId, String name, String groupId, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .editVoteGroup(userToken, voteId, name, groupId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void deleteVoteGroup(String userToken, String groupId, String voteId, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .deleteVoteGroup(userToken, groupId, voteId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void turnVoteMoney(String userToken, String tempId, double voteMoney,
                                     String voteId, String groupId,final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .turnVoteMoney(userToken, tempId, voteMoney, voteId, groupId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void editMeetingNumber(String userToken, String voteId, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .editMeetingNumber(userToken, voteId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    /**
     * 生成参会编号
     * @param userToken
     * @param voteId
     * @param projectId
     * @param voteRule
     * @param callBack
     */
    public static void getMeetingNumber(String userToken, String voteId, String projectId,
            String voteRule, final ResultCallBack<String> callBack) {
//        String memberJson = "";
//        JsonArray array = (JsonArray) new JsonArray();
//        for (CrediterUser item : userList){
//            JsonObject object = new JsonObject();
//            object.addProperty("creditorId", item.getCreditorid());
//            object.addProperty("creditorName", item.getCreditorname());
//            object.addProperty("voteMoney", item.getVoteMoney());
//            object.addProperty("confirmedMoney", item.getConfirmAmount());
//            object.addProperty("kindGuarantee", item.getKindGuarantee() == 1);
//            object.addProperty("kindOrdinary", item.getKindOrdinary() == 1);
//            object.addProperty("kindOther", item.getKindOther() == 1);
//            object.addProperty("kinkTex", item.getKindTax() == 1);
//            object.addProperty("avatar", item.getAvatar());
//            array.add(object);
//        }
//        memberJson = array.toString();
        Network.getAPIService()
                .getMeetingNumber(userToken, voteId, projectId, voteRule)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null) {
                                callBack.onSuccess("请求成功");
                            }
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    /**
     * 生成群组参会编号
     * @param userToken
     * @param groupList
     * @param callBack
     */
    public static void getGroupMeetingNumber(String userToken, List<VoteGroup> groupList, final ResultCallBack<JsonArray> callBack) {
        String memberJson = "";
        JsonArray jsonArray = new JsonArray();
        for(VoteGroup parent : groupList){
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("groupName", parent.getName());
            JsonArray array = new JsonArray();
            for (CrediterUser item : parent.getMembers()){
                JsonObject object = new JsonObject();
                object.addProperty("creditorId", item.getCreditorid());
                object.addProperty("creditorName", item.getCreditorname());
                object.addProperty("voteMoney", item.getVoteMoney());
                object.addProperty("confirmedMoney", item.getConfirmAmount());
                object.addProperty("kindGuarantee", item.getKindGuarantee() == 1);
                object.addProperty("kindOrdinary", item.getKindOrdinary() == 1);
                object.addProperty("kindOther", item.getKindOther() == 1);
                object.addProperty("kinkTex", item.getKindTax() == 1);
                object.addProperty("avatar", item.getAvatar());
                array.add(object);
            }
            jsonObject.add("members",  array);

            jsonArray.add(jsonObject);
        }
        memberJson = jsonArray.toString();
        Network.getAPIService()
                .getGroupMeetingNumber(userToken, memberJson)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null) {
                                callBack.onSuccess(json.get("data").getAsJsonArray());
                            }
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void editComplete(String userToken, String voteId, String projectId, String voteRule, final ResultCallBack<String> callBack) {
        Network.getAPIService()
                .editComplete(userToken, voteId, projectId, voteRule)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

//    如果为首次进入表决人员列表页面需填入此参数用于区分分组数据和非分组数据，后续在列表页面加载更多人员将该参数置空。即该参数为进入页面初始化参数
    public static void getOrAddVoteMemberTable(String userToken, String voteId, String groupId, String page,
                                               String projectId, String voteRule, final ResultCallBack<VoteUserListBean> callBack) {
        Network.getAPIService()
                .getOrAddVoteMemberTable(userToken, voteId, groupId, projectId, page, "20", voteRule)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonObject jsonObject = json.getAsJsonObject("data").getAsJsonObject("data");
                            JsonArray groupArray = jsonObject.getAsJsonArray("list");
                            int count = jsonObject.get("totalCount").getAsInt();

                            List<CrediterUser> crediterUsers = new ArrayList<>();
                            if (groupArray != null) {
                                for (int i = 0;i < groupArray.size();i++) {
                                    CrediterUser user = new Gson().fromJson(groupArray.get(i).toString(), CrediterUser.class);
                                    crediterUsers.add(user);
                                }
                            }

                            VoteUserListBean bean = new VoteUserListBean();
                            bean.setList(crediterUsers);
                            bean.setCount(count);
//                            try {
//                                int totalCount = json.getAsJsonObject("data").get("memberCount").getAsInt();
//                                bean.totalCount = totalCount;
//                            }catch (Exception ex)
//                            {
//                                bean.totalCount = 0;
//                                ex.printStackTrace();
//                            }
                            if (callBack != null)
                                callBack.onSuccess(bean);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    //    如果为首次进入表决人员列表页面需填入此参数用于区分分组数据和非分组数据，后续在列表页面加载更多人员将该参数置空。即该参数为进入页面初始化参数
    public static void getOrAddVoteGroupMemberTable(String userToken, String voteId,
                                               String projectId, String voteRule, final ResultCallBack<List<VoteGroup>> callBack) {
        Network.getAPIService()
                .getOrAddVoteMemberTable(userToken, voteId, "", projectId, "1", "1", voteRule)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            JsonElement element = json.get("data");
                            JsonArray groupArray = null;
                            if (element != null && !element.isJsonNull())
                                groupArray = element.getAsJsonObject().get("data").getAsJsonArray();

                            List<VoteGroup> voteGroups = new ArrayList<>();
                            if (groupArray != null) {
                                voteGroups = new ArrayList<>();
                                for (int i = 0;i < groupArray.size();i++) {
                                    VoteGroup group = new Gson().fromJson(groupArray.get(i).toString(), VoteGroup.class);
                                    int totalCount = json.getAsJsonObject("data").get("memberCount").getAsInt();
                                    group.setTotalNum(totalCount);
                                    voteGroups.add(group);
                                }
                            }
                            try {
                                int totalCount = json.getAsJsonObject("data").get("memberCount").getAsInt();

                            }catch (Exception ex)
                            {
                                ex.printStackTrace();
                            }

                            if (callBack != null)
                                callBack.onSuccess(voteGroups);
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void addGroupVoteMember(String userToken, HashMap<String, String> hashParms, int type, String voteId,
                                          String projectId, String groupId, List<CrediterUser> userList,
                                          boolean groupKindOrdinary, boolean groupKindGuarantee, boolean groupKindOther, final ResultCallBack<String> callBack) {

        Integer committee = hashParms.get("committee") == null ? null : (Integer.valueOf(hashParms.get("committee")));
        Integer onlineStatus = hashParms.get("onlineStatus") == null ? null : (hashParms.get("onlineStatus").equalsIgnoreCase("true") ? 1 : 0);
        Integer kindTax = hashParms.get("kindTax") == null ? null : (hashParms.get("kindTax").equalsIgnoreCase("true") ? 1 : 0);
        Integer kindGuarantee = hashParms.get("kindGuarantee") == null ? null : (hashParms.get("kindGuarantee").equalsIgnoreCase("true") ? 1 : 0);
        Integer kindOther = hashParms.get("kindOther") == null ? null : (hashParms.get("kindOther").equalsIgnoreCase("true") ? 1 : 0);
        Integer kindOrdinary = hashParms.get("kindOrdinary") == null ? null : (hashParms.get("kindOrdinary").equalsIgnoreCase("true") ? 1 : 0);
        Double minAmountDeclared = hashParms.get("minAmountDeclared") == null ? null : Double.valueOf(hashParms.get("minAmountDeclared"));
        Double maxAmountDeclared = hashParms.get("maxAmountDeclared") == null ? null : Double.valueOf(hashParms.get("maxAmountDeclared"));
        Double minConfirmAmount = hashParms.get("minConfirmAmount") == null ? null : Double.valueOf(hashParms.get("minConfirmAmount"));
        Double maxConfirmAmount = hashParms.get("maxConfirmAmount") == null ? null : Double.valueOf(hashParms.get("maxConfirmAmount"));
        String processStatus = hashParms.get("processStatus") == null ? null : hashParms.get("processStatus");
        String creditorName = hashParms.get("creditorName") == null ? null : hashParms.get("creditorName");

        LinkedHashMap<String, String> parms = new LinkedHashMap<>();
        parms.put("userToken", userToken);
        parms.put("voteId", voteId);
        parms.put("type", type + "");
        parms.put("projectId", projectId);
        parms.put("groupId", groupId);
        parms.put("groupKindOrdinary", (groupKindOrdinary ? 1 : 0) + "");
        parms.put("groupKindGuarantee", (groupKindGuarantee ? 1 : 0) + "");
        parms.put("groupKindOther", (groupKindOther ? 1 : 0) + "");

        if (committee != null) parms.put("role", committee + "");
        if (onlineStatus != null) parms.put("onlineStatus", onlineStatus + "");
        if (kindTax != null) parms.put("kindTax", kindTax + "");
        if (kindGuarantee != null) parms.put("kindGuarantee", kindGuarantee + "");
        if (kindOther != null) parms.put("kindOther", kindOther + "");
        if (kindOrdinary != null) parms.put("kindOrdinary", kindOrdinary + "");
        if (minAmountDeclared != null && minAmountDeclared != 0f) parms.put("minAmountDeclared", minAmountDeclared + "");
        if (maxAmountDeclared != null && maxAmountDeclared != 0f) parms.put("maxAmountDeclared", maxAmountDeclared + "");
        if (minConfirmAmount != null && minConfirmAmount != 0f) parms.put("minConfirmAmount", minConfirmAmount + "");
        if (maxConfirmAmount != null && maxConfirmAmount != 0f) parms.put("maxConfirmAmount", maxConfirmAmount + "");
        if (!TextUtils.isEmpty(processStatus)) parms.put("processStatus", processStatus + "");
        if (!TextUtils.isEmpty(creditorName)) parms.put("creditorName", creditorName + "");

        String memIds = "";
        if (userList != null) {
            for (CrediterUser user : userList)
                if (!TextUtils.isEmpty(user.getCreditorid()))
                    memIds += "," + user.getCreditorid();
        }
        if (memIds.length() > 0)
            memIds = memIds.substring(1);

        parms.put("memIds", memIds);
        Network.getAPIService()
                .addVoteMember(parms)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }

    public static void addVoteMember(String userToken, HashMap<String, String> hashParms, int type, String voteId, String projectId, List<CrediterUser> userList, final ResultCallBack<String> callBack) {

        Integer committee = hashParms.get("committee") == null ? null : (Integer.valueOf(hashParms.get("committee")));
//        Integer onlineStatus = hashParms.get("onlineStatus") == null ? null : (hashParms.get("onlineStatus").equalsIgnoreCase("true") ? 1 : 0);
//        Integer kindTax = hashParms.get("kindTax") == null ? null : (hashParms.get("kindTax").equalsIgnoreCase("true") ? 1 : 0);
//        Integer kindGuarantee = hashParms.get("kindGuarantee") == null ? null : (hashParms.get("kindGuarantee").equalsIgnoreCase("true") ? 1 : 0);
//        Integer kindOther = hashParms.get("kindOther") == null ? null : (hashParms.get("kindOther").equalsIgnoreCase("true") ? 1 : 0);
//        Integer kindOrdinary = hashParms.get("kindOrdinary") == null ? null : (hashParms.get("kindOrdinary").equalsIgnoreCase("true") ? 1 : 0);
//        Double minAmountDeclared = hashParms.get("minAmountDeclared") == null ? null : Double.valueOf(hashParms.get("minAmountDeclared"));
//        Double maxAmountDeclared = hashParms.get("maxAmountDeclared") == null ? null : Double.valueOf(hashParms.get("maxAmountDeclared"));
//        Double minConfirmAmount = hashParms.get("minConfirmAmount") == null ? null : Double.valueOf(hashParms.get("minConfirmAmount"));
//        Double maxConfirmAmount = hashParms.get("maxConfirmAmount") == null ? null : Double.valueOf(hashParms.get("maxConfirmAmount"));
//        String processStatus = hashParms.get("processStatus") == null ? null : hashParms.get("processStatus");
//        String creditorName = hashParms.get("creditorName") == null ? null : hashParms.get("creditorName");
//
//        LinkedHashMap<String, String> parms = new LinkedHashMap<>();
//        parms.put("userToken", userToken);
//        parms.put("voteId", voteId);
        hashParms.put("type", type + "");
//        parms.put("projectId", projectId);
//
//        if (committee != null) hashParms.put("role", committee + "");
//        if (onlineStatus != null) parms.put("onlineStatus", onlineStatus + "");
//        if (kindTax != null) parms.put("kindTax", kindTax + "");
//        if (kindGuarantee != null) parms.put("kindGuarantee", kindGuarantee + "");
//        if (kindOther != null) parms.put("kindOther", kindOther + "");
//        if (kindOrdinary != null) parms.put("kindOrdinary", kindOrdinary + "");
//        if (minAmountDeclared != null && minAmountDeclared != 0f) parms.put("minAmountDeclared", minAmountDeclared + "");
//        if (maxAmountDeclared != null && maxAmountDeclared != 0f) parms.put("maxAmountDeclared", maxAmountDeclared + "");
//        if (minConfirmAmount != null && minConfirmAmount != 0f) parms.put("minConfirmAmount", minConfirmAmount + "");
//        if (maxConfirmAmount != null && maxConfirmAmount != 0f) parms.put("maxConfirmAmount", maxConfirmAmount + "");
//        if (processStatus != null) parms.put("processStatus", processStatus + "");
//        if (!TextUtils.isEmpty(creditorName)) parms.put("creditorName", creditorName + "");

        String memIds = "";
        if (userList != null) {
            for (CrediterUser user : userList)
                if (!TextUtils.isEmpty(user.getCreditorid()))
                    memIds += "," + user.getCreditorid();
        }
        if (memIds.length() > 0)
            memIds = memIds.substring(1);

        hashParms.put("memIds", memIds);
        Network.getAPIService()
                .addVoteMember(hashParms)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            if (callBack != null)
                                callBack.onFailed(json.get("msg").getAsString());
                        } else {
                            if (callBack != null)
                                callBack.onSuccess("请求成功");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        if (callBack != null)
                            callBack.onFailed("请求失败");
                    }
                });
    }
}
